<?php
class localisation_index extends MX_Controller{

	//valeur par defaut Paris
	public $Region='75,77,78,91,92,93,94,95';
	public $Nom_Region='Ilê de France';
	public $departement=75;
	public $Postalcode=75001;
	
	public function index(){
		
		if(isset($_POST['name']) and isset($_POST['id'])){
			
			if(empty($_POST["id"])) return false;
			switch ($_POST['name']) {
				case 'annonce_departement':
					$var_Dept='D_'.str_replace(',','_',$_POST['id']);
					
					$$var_Dept='';
					$this->load->driver('cache');
					
					// Save into the cache for 5 minutes
					if ( ! $$var_Dept = $this->cache->file->get($var_Dept))
					{
						
						$localisation =$this->model();
						$departements=$localisation->get_departement($_POST['id']);
						foreach ($departements as $departement){
							$$var_Dept.= "<option value='$departement->code_departement'>{$departement->nom}</option>";
						}
						$this->cache->file->save($var_Dept, $$var_Dept, 300);
					}
					echo $$var_Dept;
					
					break;
			    case 'departement':
			    	$var_Dept='D_'.str_replace(',','_',$_POST['id']);
					
					$$var_Dept='';
					$this->load->driver('cache');
					
					// Save into the cache for 5 minutes
					if ( ! $$var_Dept = $this->cache->file->get($var_Dept))
					{
						
						$localisation =$this->model();
						$departements=$localisation->get_departement($_POST['id']);
						foreach ($departements as $departement){
							$$var_Dept.= "<option value='$departement->code_departement'>{$departement->nom}</option>";
						}
						$this->cache->file->save($var_Dept, $$var_Dept, 300);
					}
					echo $$var_Dept;
			    case 'ville':
			    	
			    	$var_Ville='V_'.$_POST['id'];
			    		
			    	$$var_Ville='';
			    	$this->load->driver('cache');
			    		
			    	// Save into the cache for 5 minutes
			    	if ( ! $$var_Ville = $this->cache->file->get($var_Ville))
			    	{
			    	
			    		$localisation =$this->model();
						$villes=$localisation->get_ville($_POST['id']);
				    	foreach ($villes as $ville){
				    		$$var_Ville.= "<option value='{$ville->Cp}'>{$ville->N_Com_min} - {$ville->Cp}</option>";
				    	}
			    		$this->cache->file->save($var_Ville, $$var_Ville, 300);
			    	}
			    	echo $$var_Ville;
			    	
			    	
			    	
			        break;
			}
		}
	}
	private function getlocationfromDB($ip) {
		$row=$this->model()->getlocationfromDB($ip);
		if($row){
			$match['City']=$row->city;
			if($row->postalCode) $match['Postalcode']=$row->postalCode;
			else {
				$cp=$this->model()->get_cp_by_city($row->city);
				if($cp)$match['Postalcode']=$cp;
			}
			return $match;
		}
		return false;
	}
	public function getlocationCodePostal($codePostal) {
		if(empty($codePostal))return false;
		$this->Postalcode=$codePostal;
		$this->departement=$this->model()->get_departement_by_cp($codePostal);
		$this->Region=$this->model()->get_region_by_departement($this->departement);;
		$Region=$this->model()->get_region($this->departement);
		$this->Nom_Region=$Region[0]->nom_region;
		return $this;
	}
	
	public function getlocation($ip='') {
		$ipTest=str_replace('.','',$ip);
		/* deactive geolocip pour l'instinct
		if(empty($ip) or !is_numeric($ipTest)){
			if ( isset($_SERVER["REMOTE_ADDR"]) )    {
				$ip=$_SERVER["REMOTE_ADDR"];
			} else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) )    {
				$ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
			} else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    {
				$ip=$_SERVER["HTTP_CLIENT_IP"];
			}
		}
		$locationurl = 'http://www.geolocip.com/?ip='.$ip;
	
		if($locationf = fopen($locationurl,'r')) {
			ob_start();
			fpassthru($locationf);
			$locationhtml = strip_tags(ob_get_contents(),'<dt><dd>');
			ob_end_clean();
			fclose($locationf);
		}
		preg_match_all ("/<dt>(.*)<\/dt>/U", $locationhtml, $matchDt);
	
		preg_match_all ("/<dd>(.*)<\/dd>/U", $locationhtml, $matchDd);
		*/
		$match=array();
		if( isset($matchDt[1]) and !empty($matchDt[1]) and
		isset($matchDd[1]) and !empty($matchDd[1]) ){
			foreach($matchDt[1] as $index=>$Dt){
				$dt=str_replace(array(' :',' '),'',$Dt);
				if($matchDd[1][$index]) $match[$dt]=$matchDd[1][$index];
				if(!isset($match['Postalcode']) and isset($match['City'])){
					$cp=$this->model()->get_cp_by_city($match['City']);
					if($cp)$match['Postalcode']=$cp;
				}
			}
		}
		if(empty($match) and !isset($match['Postalcode']) and !isset($match['City'])){
			$match=self::getlocationfromDB($ip);
		}
		if(isset($match['Postalcode']) and !empty($match['Postalcode'])){
			$departementCode=$this->model()->get_departement_by_cp($match['Postalcode']);
			if(!empty($departementCode))$match['departement']=$departementCode;
			$Region=$match['Region']='';
			$Region=$this->model()->get_region($departementCode);
			if(!empty($Region))$match['Region']=$Region[0]->code_departement;
		}
		if(!empty($match))
			foreach ($match as $key=>$val)
				$this->$key=$val;
		return $this;
	}
	public function region(){
		$localisation =$this->model()->get_region();
		return $localisation;
	}

	private function departement($codeDepartement=''){
		$localisation =$this->model()->get_departement($codeDepartement);
		return $localisation;
	}

	private function ville($codeDepartement){
		$localisation =$this->model()->get_ville($codeDepartement);
		return $localisation;
	}
	
	
	public function OptionRegion(){
		$regionList='';
		foreach (self::region() as $region){
			$s='';
			if($this->Region==$region->code_departement ){
				$s="selected='selected'";
			}
			$testDepartement=explode(',',$region->code_departement);
			if($this->departement and in_array($this->departement,$testDepartement)){
				$s="selected='selected'";
			}
			$regionList.="<option value='{$region->code_departement}' $s>{$region->nom_region}</option>\n";
		}
		return $regionList;
	}
	public function OptionDepartement(){
		$departementList='';
		foreach (self::departement($this->Region) as $departement){
			$s='';
			if(trim($this->departement)==trim($departement->code_departement) ) $s='selected="selected"';
			$departementList.="<option value='{$departement->code_departement}' $s>{$departement->nom}</option>\n";
		}
		return $departementList;
	}
	public function OptionVille($Postalcode=''){
		if(empty($Postalcode)) $Postalcode=$this->Postalcode;
		$villeList='';
		foreach (self::ville($this->departement) as $ville){
			$s='';
			if(trim($Postalcode)==trim($ville->Cp)) $s='selected="selected"';
			$villeList.="<option value='{$ville->Cp}' $s>{$ville->N_Com_min} - {$ville->Cp}</option>\n";
		}
		return $villeList;
	}

	public function OptionDepartement2Code(){
		$Region=$this->model()->get_region_by_departement($this->departement);
		$this->Region=$Region;
		$listDepartement=self::OptionDepartement();
		return $listDepartement;		
	}
	public function set_location_by_region($Region=''){
		if($Region=='')$Region=$this->Region;
		if(!$Region) return false;
		$this->Region=$Region;		
		$this->Postalcode='';
		$this->departement='';
		$extract_departement=explode(',',$Region);
		$departement_first=isset($extract_departement[0])?$extract_departement[0]:false;
		
		$Region=$this->model()->get_region($departement_first);
		$this->Nom_Region=$Region[0]->nom_region;
		return $this;
	}
}